/**
 * Created by meiguizhinian on 2018/7/17
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//单例/全静态/枚举
ivy.TeachManager = {
	getInstance: function () {
		return this;
	},

	clear : function() {
		this.currentRunningTeachIndex_ = -1;
		this.teachLayer_ = null;
	},

	runTeachByIndex : function(index) {
		let teachNode = ivy.UIManager.getInstance().getUITeachNode();
		//每次执行教学 教学层节点应该都为空节点
		cc.assert(teachNode.getChildrenCount()===0);
		if (index >= ivy.TeachDataManager.getInstance().getTeachDataCount())
			return;
		this.currentRunningTeachIndex_ = index;
		this.currentRunningTeachChildIndex_ = 0;
		this.teachLayer_ = ivy.UITeachLayerBase.create();
		teachNode.addChild(this.teachLayer_);

		let tdm = ivy.TeachDataManager.getInstance();
		let curSourceTeachType = tdm.getTeachDataSourceTypeByIndex(this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		if (this.teachSourceTypeCallBackDataMap_.has(curSourceTeachType)) {
			this.teachSourceTypeCallBackDataMap_.get(curSourceTeachType).onEnter_(this.teachLayer_, this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		}
	},
	isTeaching : function() {
		return this.currentRunningTeachIndex_ >= 0;
	},
	setTeachEnd : function() {
		this.currentRunningTeachIndex_ = -1;
		let teacher = this.teachLayer_;
		this.teachLayer_ = null;
		ivy.Tools.pushFunctionToCocosThread(function() {
			teacher.removeFromParent();
			if (this.endTeachFunc_)
				this.endTeachFunc_();
		}.bind(this));
	},
	doTouchBegan : function(sender, touch, event) {
		let tdm = ivy.TeachDataManager.getInstance();
		let curSourceTeachType = tdm.getTeachDataSourceTypeByIndex(this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		if (this.teachSourceTypeCallBackDataMap_.has(curSourceTeachType)) {
			return this.teachSourceTypeCallBackDataMap_.get(curSourceTeachType).onTouchBeganCallBack_(sender, touch, event, this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		}
		return false;
	},
	doTouchMoved : function(sender, touch, event) {
		let tdm = ivy.TeachDataManager.getInstance();
		let curSourceTeachType = tdm.getTeachDataSourceTypeByIndex(this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		if (curSourceTeachType < 0)
			return;
		if (this.teachSourceTypeCallBackDataMap_.has(curSourceTeachType)) {
			this.teachSourceTypeCallBackDataMap_.get(curSourceTeachType).onTouchMovedCallBack_(sender, touch, event, this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		}
	},
	doTouchEnded : function(sender, touch, event) {
		let tdm = ivy.TeachDataManager.getInstance();
		let curSourceTeachType = tdm.getTeachDataSourceTypeByIndex(this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		if (curSourceTeachType < 0)
			return;
		if (this.teachSourceTypeCallBackDataMap_.has(curSourceTeachType)) {
			this.teachSourceTypeCallBackDataMap_.get(curSourceTeachType).onTouchEndedCallBack_(sender, touch, event, this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		}
	},
	update : function(sender, dt) {
		let tdm = ivy.TeachDataManager.getInstance();
		let curSourceTeachType = tdm.getTeachDataSourceTypeByIndex(this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
		if (this.teachSourceTypeCallBackDataMap_.has(curSourceTeachType)) {
			this.teachSourceTypeCallBackDataMap_.get(curSourceTeachType).onUpdateCallBack_(sender, this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_, dt);
		}
	},
	registerTeachSourceTypeCallBack : function(typeIndex, callBackData) {
		this.teachSourceTypeCallBackDataMap_.set(typeIndex, callBackData);
	},
	runNextTeachData : function() {
		ivy.UIManager.getInstance().setIgnoreScrollCheck(false);
		let tdm = ivy.TeachDataManager.getInstance();
		++this.currentRunningTeachChildIndex_;
		let teachChildCount= tdm.getTeachDataCountByIndex(this.currentRunningTeachIndex_);
		if (this.currentRunningTeachChildIndex_ < teachChildCount) {
			if (this.teachLayer_)
				this.teachLayer_.setTimerPartLife(0);
			let curSourceTeachType = tdm.getTeachDataSourceTypeByIndex(this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
			if (this.teachSourceTypeCallBackDataMap_.has(curSourceTeachType)) {
				this.teachSourceTypeCallBackDataMap_.get(curSourceTeachType).onEnter_(this.teachLayer_, this.currentRunningTeachIndex_, this.currentRunningTeachChildIndex_);
			}
		} else {
			this.setTeachEnd();
		}
	},
	setTeachEndCallback : function(func) {
		this.endTeachFunc_ = func;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	currentRunningTeachIndex_ : -1,
	currentRunningTeachChildIndex_ : -1,
	teachSourceTypeCallBackDataMap_ : new LLDictionary(),//std::map<int32_t, TeachSourceTypeCallBackData>
	teachLayer_ : null,//UITeachLayerBase*
	endTeachFunc_ : null,//std::function<void()>
};
